#include "NewProjectDlg.h"

#include "comLogManager.h"
#include "taskLuaMgr.h"
#include "ttcodeStateChart.h"


#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>


NewProjectDlg::NewProjectDlg( wxWindow* parent )
:NewProjectDialog(parent)
,m_Index(0)
,m_LocationPos(0)
{
	this->loadTree();
	this->loadPanel();
	this->loadText();
	this->loadLocation();
	this->loadOk();
}

void NewProjectDlg::genProject(std::string &doc_name,std::string &doc_home)
{
	doc_name.clear();
	doc_home.clear();
	std::string name = m_textCtrlName->GetValue();
	if(name.empty())
	{
		return ;
	}
	int sel = m_choiceLocation->GetCurrentSelection();
	std::string path = m_choiceLocation->GetString(sel);
	if(path.empty())
	{
		return ;
	}
	globalTempMgr->genProject(m_CurTemp,name,path);
	doc_name = path + "/" + name + ".ttcode";
	doc_home = path;
}

void NewProjectDlg::loadTree( void )
{
	try
	{
		m_root = m_treeCtrlProjectType->AddRoot("Root");
		m_general = m_treeCtrlProjectType->AppendItem(m_root,"General Project");
		m_cxx = m_treeCtrlProjectType->AppendItem(m_general,"C/C++" );
		m_ruby = m_treeCtrlProjectType->AppendItem(m_general,"Ruby" );
		m_lua = m_treeCtrlProjectType->AppendItem(m_general,"Lua" );
		m_treeCtrlProjectType->SelectItem(m_cxx);
		m_treeCtrlProjectType->ExpandAll();
	}
	catch(std::exception e)
	{
		ttcode_log << "exception:" << e.what() << endl;
	}
	m_CurTreeItem = "c/c++";
}

void NewProjectDlg::loadPanel( void )
{
	try
	{
		m_listCtrlPanel->DeleteAllItems();
		ttTempList list = globalTempMgr->getTemp();
		bool first = true;
		BOOST_FOREACH( TiXmlElement *elem,list )
		{
			TiXmlElement *lang = elem->FirstChildElement("lang");
			if( NULL == lang )
			{
				continue;
			}
			const char * pLang = lang->GetText();
			if( NULL == pLang )
			{
				continue;
			}
			string strLang(pLang);
			ttcode_log << "strLang=<"<< strLang << ">" << endl;
			if( m_CurTreeItem != strLang )
			{
				continue;
			}
			TiXmlElement *name = elem->FirstChildElement("name");
			if( NULL == name )
			{
				continue;
			}
			const char * pName = name->GetText();
			if( NULL == pName )
			{
				continue;
			}

			TiXmlElement *id = elem->FirstChildElement("id");
			if( NULL == id )
			{
				continue;
			}
			const char * pId = id->GetText();
			if( NULL == pId )
			{
				continue;
			}
			wxListItem item;
			item.SetId(m_Index++);
			item.SetText(pName);
			item.SetImage(globalIiMgr->getId("execute"));
			item.SetData( (void*)(pId) );
			if( first )
			{
				item.SetState( wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED );
				m_CurTemp = string(pId);
			}
			m_listCtrlPanel->InsertItem(item);
			first = false;
		}
	}
	catch(std::exception e)
	{
		ttcode_log << "exception:" << e.what() << endl;
	}
}
void NewProjectDlg::loadText( void )
{
	try
	{
		ttTempList list = globalTempMgr->getTemp();
		BOOST_FOREACH( TiXmlElement *elem,list )
		{
			TiXmlElement *id = elem->FirstChildElement("id");
			if( NULL == id )
			{
				continue;
			}
			const char * pId = id->GetText();
			if( NULL == pId )
			{
				continue;
			}
			string strId(pId);
//			ttcode_log << "strId=<"<< strId << ">" << endl;
			if( m_CurTemp != strId )
			{
				continue;
			}
			TiXmlElement *details = elem->FirstChildElement("details");
			if( NULL == details )
			{
				continue;
			}
			const char * pDetails = details->GetText();
			if( NULL == pDetails )
			{
				continue;
			}
			m_staticTextGuildText->SetLabel(string(pDetails));
			break;
		}
	}
	catch(std::exception e)
	{
		ttcode_log << "exception:" << e.what() << endl;
	}
}



void NewProjectDlg::loadLocation( void )
{
	try
	{
		m_choiceLocation->Insert( globalTTEnv["pwd"],m_LocationPos++ );
		m_choiceLocation->Select( m_LocationPos - 1 );
	}
	catch(std::exception e)
	{
		ttcode_log << "exception:" << e.what() << endl;
	}
}






void NewProjectDlg::loadOk( void )
{
	try
	{
		m_sdbSizer2OK->Enable(false);
	}
	catch(std::exception e)
	{
		ttcode_log << "exception:" << e.what() << endl;
	}
}



void NewProjectDlg::m_treeCtrlProjectTypeOnTreeSelChanged( wxTreeEvent& event )
{
	const wxTreeItemId item = event.GetItem();
	if( item == this->m_cxx )
	{
		ttcode_log << "item c/c++" << endl;
		this->m_CurTreeItem = "c/c++";
	}
	else if( item == this->m_ruby )
	{
		ttcode_log << "item ruby" << endl;
		this->m_CurTreeItem = "ruby";
	}
	else if( item == this->m_lua )
	{
		ttcode_log << "item lua" << endl;
		this->m_CurTreeItem = "lua";
	}
	else
	{
		ttcode_log << "item not handling item" << endl;
		return;
	}
	this->loadPanel();
}
void NewProjectDlg::m_listCtrlPanelOnListItemSelected( wxListEvent& event )
{
	wxListItem item = event.GetItem();
	wxUIntPtr data = item.GetData();
	char *pId = (char*)(data);
	if( NULL == pId)
	{
		return ;
	}
	this->m_CurTemp = string(pId);
//	ttcode_log << "pId = <" << pId << ">" << endl;
	this->loadText();
}
void NewProjectDlg::m_textCtrlNameOnText( wxCommandEvent& event )
{
	m_sdbSizer2OK->Enable();
}


